using Android.Content;
using Android.Views;
using Android.OS;
using AndroidX.Fragment.App;
using Google.Android.Material.BottomNavigation;
using AndroidX.Core.Content;
using Android.Graphics;
using MagicBooks.Fragments;
using Fragment = AndroidX.Fragment.App.Fragment;

namespace MagicBooks
{
    [Activity(Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : FragmentActivity
    {
        // 缓存 Fragment 实例
        private Fragment? homeFragment;
        private Fragment? booksFragment;
        private Fragment? settingsFragment;
        private Fragment? currentFragment;

        protected override void OnCreate(Bundle? savedInstance)
        {
            base.OnCreate(savedInstance);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            
            BottomNavigationView navigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);

            // 初始化第一个 Fragment
            homeFragment = new HomeFragment();
            SupportFragmentManager.BeginTransaction()
                .Add(Resource.Id.container, homeFragment, "home")
                .Commit();
            currentFragment = homeFragment;

            if (navigation != null)
                navigation.ItemSelected += (sender, e) =>
                {
                    Fragment? targetFragment = null;
                    string? tag = null;
                    switch (e.Item.ItemId)
                    {
                        case Resource.Id.nav_home:
                            if (homeFragment == null)
                                homeFragment = new HomeFragment();
                            targetFragment = homeFragment;
                            tag = "home";
                            break;
                        case Resource.Id.nav_books:
                            if (booksFragment == null)
                                booksFragment = new BooksFragment();
                            targetFragment = booksFragment;
                            tag = "books";
                            break;
                        case Resource.Id.nav_settings:
                            if (settingsFragment == null)
                                settingsFragment = new SettingsFragment();
                            targetFragment = settingsFragment;
                            tag = "settings";
                            break;
                    }

                    if (targetFragment != null && targetFragment != currentFragment)
                    {
                        var transaction = SupportFragmentManager.BeginTransaction();
                        // 隐藏当前 Fragment
                        if (currentFragment != null)
                            transaction.Hide(currentFragment);

                        // 如果目标 Fragment 未添加，则添加
                        if (!targetFragment.IsAdded)
                            transaction.Add(Resource.Id.container, targetFragment, tag);
                        else
                            transaction.Show(targetFragment);

                        transaction.Commit();
                        currentFragment = targetFragment;
                    }
                };
        }
        
    }
}